package com.example.muldatasource.controller;

import com.alibaba.fastjson.JSON;
import com.example.muldatasource.dao.first.StudentMapper;
import com.example.muldatasource.dao.second.LessonMapper;
import com.example.muldatasource.domain.first.Student;
import com.example.muldatasource.domain.second.Lesson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ProjectName: muldatasource
 * @Author: qinjiwei
 * @Description:
 * @Date: 2020/6/28 18:19
 * @Version: 1.0
 */
@RestController
@RequestMapping("/api")
public class DemoController {

    Logger log = LoggerFactory.getLogger(DemoController.class);

    @Autowired
    StudentMapper studentMapper;

    @Autowired
    LessonMapper lessonMapper;

    @RequestMapping("/source")
    public void getdemo(Integer id) throws Exception {
        List<Student> list1 = studentMapper.queryStudents();
        List<Lesson> list2  = lessonMapper.findById(id);

        log.info("student:{} lesson:{}", JSON.toJSONString(list1), JSON.toJSONString(list2));
        throw new Exception("yicl");
    }

    @RequestMapping("/add")
    @Transactional
    public void setInfo() throws Exception {
        Student student = new Student();
        student.setId("666");
        student.setName("6666");

        studentMapper.insert(student);

        Lesson lesson = new Lesson();
        lesson.setId("777");
        lesson.setName("7777");
        lessonMapper.insert(lesson);

        throw new RuntimeException("yicl");
    }
}
